package edu.tum.in.campar.twodui.studentcanteen;

import java.util.ArrayList;

import edu.tum.in.campar.twodui.studentcanteen.model.DailyMenu;
import edu.tum.in.campar.twodui.studentcanteen.model.DataProvider;
import edu.tum.in.campar.twodui.studentcanteen.model.Meal;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;

public class TVDateAndMealActivity extends ExpandableListActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		ExpandableListView list = getExpandableListView();
		final ArrayList<DailyMenu> menus = DataProvider.get().getData();
		
		list.setAdapter(new BaseExpandableListAdapter() {
			
			@Override
			public boolean isChildSelectable(int groupPosition, int childPosition) {
				return false;
			}
			
			@Override
			public boolean hasStableIds() {
				return false;
			}
			
			@Override
			public View getGroupView(int groupPosition, boolean isExpanded,
					View convertView, ViewGroup parent) {
				View view = getLayoutInflater().inflate(
						android.R.layout.simple_expandable_list_item_1, null);
				view.setMinimumHeight(64);

				((TextView) view.findViewById(android.R.id.text1))
						.setText(menus.get(groupPosition).getDay());
				return view;
			}
			
			@Override
			public long getGroupId(int groupPosition) {
				return groupPosition;
			}
			
			@Override
			public int getGroupCount() {
				return menus.size();
			}
			
			@Override
			public Object getGroup(int groupPosition) {
				return menus.get(groupPosition);
			}
			
			@Override
			public int getChildrenCount(int groupPosition) {
				return  menus.get(groupPosition).getMeals().size();
			}
			
			@Override
			public View getChildView(int groupPosition, int childPosition,
					boolean isLastChild, View convertView, ViewGroup parent) {
				View view = getLayoutInflater().inflate(
						android.R.layout.simple_expandable_list_item_2, null);
				view.setMinimumHeight(64);

				Meal meal = (Meal) getChild(groupPosition, childPosition);

    			TextView text = (TextView)view.findViewById(android.R.id.text1);
    			text.setText(meal.getTitle());
    			
    			text = (TextView)view.findViewById(android.R.id.text2);
    			text.setText(meal.getPrice() + "\u20AC");
    			text.setGravity(Gravity.RIGHT);
    			
				return view;
			}

			
			@Override
			public long getChildId(int groupPosition, int childPosition) {
				return childPosition;
			}
			
			@Override
			public Object getChild(int groupPosition, int childPosition) {
				DailyMenu menu = menus.get(groupPosition);
				return menu.getMeals().get(childPosition);
			}
		});
	}
    
    
	
}
